package com.tcs.gamed.server.model;

import java.io.Serializable;
import java.util.Date;

import com.google.appengine.api.datastore.Key;
import com.tcs.gamed.shared.model.ItemType;

import org.slim3.datastore.Attribute;
import org.slim3.datastore.CreationDate;
import org.slim3.datastore.Datastore;
import org.slim3.datastore.Model;
import org.slim3.datastore.ModelRef;
import org.slim3.datastore.ModificationDate;

/**
 * Base class of all folder items.
 * @author karasu
 *
 */
@Model(schemaVersionName = "slim3.schemaVersionFolderItem", schemaVersion = 1)
public class FolderItem implements Serializable {

    private static final long serialVersionUID = 4196598333736334415L;

    @Attribute(primaryKey = true)
    private Key key;

    @Attribute(version = true)
    private Long version;
    
    @Attribute(listener = ModificationDate.class)
    private Date updatedAt;
    
    @Attribute(listener = CreationDate.class)
    private Date createdAt;
    
    private String name;
    
    @Attribute(lob = true)
    private String comment;
    
    private ModelRef<Folder> parentRef
        = new ModelRef<Folder>(Folder.class);
    
    private ItemType type;
    
    /**
     * Returns the key.
     *
     * @return the key
     */
    public Key getKey() {
        return key;
    }

    /**
     * Sets the key.
     *
     * @param key
     *            the key
     */
    public void setKey(Key key) {
        this.key = key;
    }

    /**
     * Returns the version.
     *
     * @return the version
     */
    public Long getVersion() {
        return version;
    }

    /**
     * Sets the version.
     *
     * @param version
     *            the version
     */
    public void setVersion(Long version) {
        this.version = version;
    }

    public Date getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(Date updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public ModelRef<Folder> getParentRef() {
        return parentRef;
    }

    public ItemType getType() {
        return type;
    }

    public void setType(ItemType type) {
        this.type = type;
    }
    
    public com.tcs.gamed.shared.model.FolderItem makeSharedFolderItem() {
        final Key parentKey = getParentRef().getKey();
        final com.tcs.gamed.shared.model.FolderItem item
            = new com.tcs.gamed.shared.model.FolderItem();
        item.setKey(Datastore.keyToString(getKey()));
        item.setName(getName());
        item.setType(getType());
        item.setUpdatedAt(getUpdatedAt());
        item.setCreatedAt(getCreatedAt());
        item.setComment(getComment());
        item.setParentKey(
            (parentKey == null) ? null : Datastore.keyToString(parentKey));
        return item;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        FolderItem other = (FolderItem) obj;
        if (key == null) {
            if (other.key != null) {
                return false;
            }
        } else if (!key.equals(other.key)) {
            return false;
        }
        return true;
    }
}
